﻿using Adnc.Application.Shared;
using Adnc.Application.Shared.Dtos;
using Adnc.WebApi.Shared;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using NLog;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using WYT.Usr.Application.Contracts.Dtos;
using WYT.Usr.Application.Contracts.Dtos.User;
using WYT.Usr.Application.Contracts.Services;

namespace WYT.Usr.WebApi.Controllers
{
    /// <summary>
    /// 账户
    /// </summary>
    [Route("usr/session")]
    [ApiController]
    [AllowAnonymous]
    public class AcountController : AdncControllerBase
    {
        private readonly IUserContext _userContext;

        private readonly IAccountAppService _accountAppService;
        public AcountController(IAccountAppService accountAppService, IUserContext userContext)
        {
            _accountAppService = accountAppService;
            _userContext = userContext;
        }
        /// <summary>
        /// 登录
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("Login")]
        public async Task<WYTActionResult<UserTokenInfoDto>> Login(UserParaDto input)
        {
            return Success(await _accountAppService.LoginAsync(input));
        }

        /// <summary>
        /// 刷新token
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("RefreshToken")]
        public async Task<WYTActionResult<UserTokenInfoDto>> RefreshToken(UserRefreshTokenDto input)
        {
            return Success(await _accountAppService.RefreshAccessTokenAsync(input));
        }


        /// <summary>
        /// 发送验证码(手机号码登录）
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost("sendCode")]
        public async Task<WYTActionResult> SendSms(SimpleDto<string> input)
        {
            if (string.IsNullOrEmpty(input.Value) || !Regex.IsMatch(input.Value, @"^[1]+\d{10}"))
                return new WYTActionResult() { Code = 400, Msg = "参数错误" };

            return Success(await _accountAppService.SendSms(input.Value));
        }

        /// <summary>
        /// 手机号码登录
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost("TelLogin")]
        public async Task<WYTActionResult<UserTokenInfoDto>> TelLoginAsync(TelLoginDto input)
        {
            return Success(await _accountAppService.TelLoginAsync(input));
        }

        /// <summary>
        /// 获取分享路径参数
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpGet("sharePath")]
        public async Task<WYTActionResult<string>> GetSharePath([FromQuery]SimpleDto<long> input)
        {
            return Success(await _accountAppService.GetSharePath(input.Value));
        }

        /// <summary>
        /// 获取用户token,仅用于开发阶段
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        [HttpPost("userToken")]
        public async Task<WYTActionResult<UserTokenInfoDto>> LoginAsync(long userId)
        {
            Logger logger = LogManager.GetCurrentClassLogger();
            logger.Debug(() => "获取用户：" + userId + "的token");
            return Success(await _accountAppService.LoginAsync(userId));
        }
    }
}
